package org.brownflat.lighthouse.monitoring;

import org.apache.log4j.Logger;
import org.brownflat.lighthouse.monitoring.ci.Monitorable;
import org.brownflat.lighthouse.monitoring.monitor.AnyBrokenBuildTask;
import org.brownflat.lighthouse.monitoring.monitor.MonitoringService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Lighthouse {

    private static final Logger LOG = Logger.getLogger(Lighthouse.class);

    public static void main(String args[]) {
        Lighthouse lighthouse = new Lighthouse();
        lighthouse.run();
    }

    public Lighthouse() {
    }

    public void run() {
        Thread.currentThread().setName("Lighthouse");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config-lighthouse.xml");
        MonitoringService service = (MonitoringService) context.getBean("monitoringService");
        AnyBrokenBuildTask task = (AnyBrokenBuildTask) context.getBean("check");
        Monitorable monitorable = task.getMonitorable();
        LOG.info("*************************************************");
        LOG.info("Starting Lighthouse...");
        LOG.info("Initializing spring...");
        LOG.info("Initializing monitoring jobs...");
        service.start();
        LOG.info("Monitoring " + monitorable.getName() + " at " + monitorable.getLocation());
        LOG.info("Lighthouse started");
        LOG.info("*************************************************");

        Runtime.getRuntime().addShutdownHook(new LighthouseShutdownHook());
        try {
            while (true) {
                Thread.sleep(10000);
                LOG.trace("Lighthouse pulse.");
            }
        } catch (InterruptedException inte) {
            LOG.error(inte);
        }
        LOG.info("Lighthouse stopped.");
    }
}
